package edu.ccut.saturn.studio.authority.page;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IProject;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseTrackAdapter;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.w3c.dom.Node;

import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.Images;
import edu.ccut.saturn.studio.core.search.SaturnFileSearcher.SearchResult;
import edu.ccut.saturn.studio.core.search.dialog.TowComponentSearchInProjectDialog;
import edu.ccut.saturn.studio.core.search.dialog.TwoCompontentSearchDialog;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.core.util.eclipse.SaturnValidation;
import edu.ccut.saturn.studio.core.util.xml.SaturnW3CUtil;

/**
 * 
 * @author Bai Yucheng  2008-1-30
 *
 */
public class EditResourceDialog extends TitleAreaDialog {
	private Shell shell;
	private Text descriptionText;
	private Text valueText;
	private Text nameText;

	private String name;
	private String value;
	private String descritpion;
	
	private IProject project = null;
	
	public static int i = 0;
	
	/**
	 * Create the dialog
	 * @param parentShell
	 */
	public EditResourceDialog(Shell parentShell, IProject project) {
		super(parentShell);
		this.shell = parentShell;
		this.project = project;
	}

	/**
	 * Create contents of the dialog
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite area = (Composite) super.createDialogArea(parent);
		Composite container = new Composite(area, SWT.NONE);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.marginTop = 5;
		gridLayout.marginRight = 5;
		gridLayout.marginLeft = 5;
		gridLayout.numColumns = 3;
		container.setLayout(gridLayout);
		container.setLayoutData(new GridData(GridData.FILL_BOTH));
		
		final Label nameLabel = new Label(container, SWT.NONE);
		nameLabel.setText(Messages.getString("EditResourceDialog.name"));  //$NON-NLS-1$

		nameText = new Text(container, SWT.BORDER);
		nameText.setTextLimit(50);
		nameText.setText(name);
		nameText.addModifyListener(new ModifyListener() {
			public void modifyText(final ModifyEvent e) {
				dialogChanged();
			}
		});
		final GridData gd_nameText = new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1);
		nameText.setLayoutData(gd_nameText);
		
		final Label valueLabel = new Label(container, SWT.NONE);
		valueLabel.setText(Messages.getString("EditResourceDialog.value"));  //$NON-NLS-1$

		valueText = new Text(container, SWT.BORDER);
		valueText.setTextLimit(100);
		valueText.setText(value);
		valueText.addModifyListener(new ModifyListener() {
			public void modifyText(final ModifyEvent e) {
				dialogChanged();
			}
		});
		final GridData gd_valueText = new GridData(SWT.FILL, SWT.CENTER, true, false);
		valueText.setLayoutData(gd_valueText);
		
		
		final Label browser = new Label(container, SWT.NONE);
		final GridData gd_browser = new GridData(SWT.RIGHT, SWT.CENTER, false, false);
		browser.setLayoutData(gd_browser);
//		browser.setImage(Activator.getImage(IConstants.IMG_NODE33));
		browser.setImage(Activator.getImage(Images.AMPLIFY_PLUS));
		browser.addMouseTrackListener(new MouseTrackAdapter() {
			public void mouseEnter(final MouseEvent e) {
//				browser.setImage(Activator.getImage(IConstants.IMG_NODE34));
				browser.setImage(Activator.getImage(Images.AMPLIFY_MINUS)); 
			}
			public void mouseExit(final MouseEvent e) {
//				browser.setImage(Activator.getImage(IConstants.IMG_NODE33));  
				browser.setImage(Activator.getImage(Images.AMPLIFY_PLUS)); 
			}
		});
		
		browser.addMouseListener(new MouseAdapter() {
			public void mouseDown(final MouseEvent e) {
//				browser.setImage(Activator.getImage(IConstants.IMG_NODE34));
				browser.setImage(Activator.getImage(Images.AMPLIFY_MINUS));
				
				if (project != null) {
					String  suffixes [] = {"action","jsp"};
					TowComponentSearchInProjectDialog dialog1 = new TowComponentSearchInProjectDialog(
							shell,suffixes, project);
					dialog1.setTitle(Messages.getString("AddResourceDialog.5")); //$NON-NLS-1$
					if (dialog1.open() == Window.OK) {
						SearchResult result = (SearchResult) dialog1
								.getFirstResult();
				
							String filePath = result.getFile() + ""; //$NON-NLS-1$
							if ((result.getFile()+"").endsWith(".jar")) {
								
								Node RootNode = result.getDocument().getDocumentElement();
								Node InformationNode = SaturnW3CUtil.getFirstChildByName(RootNode, "information");
								String key = SaturnW3CUtil.getChildNodeValue(InformationNode, "key");
								valueText.setText(result.getBundleName()+ "::" + key);
							} else{
								
								String tempPath = filePath.replace(
										result.getSrcName() + "",
										"");
								valueText.setText(tempPath.replace(
										"\\", "/"));
							}
						}
					}
//					List<String> oldAllList = SearchFile.searchInSrc(project, new String[] {"jsp", "action"});   //$NON-NLS-1$ //$NON-NLS-2$
//					List<String> allList = formatPath(oldAllList);
//					String[] allArray = (String[]) allList.toArray(new String[allList.size()]);
//					
//					String[] jspArray = formatArray(allArray, "jsp");  //$NON-NLS-1$
//					
//					String[] actionArray = formatArray(allArray, "action");  //$NON-NLS-1$
//					
//					String[][] res = {allArray, jspArray, actionArray};
//				
//					FilterDialog dialog = new FilterDialog(shell, new SaturnSWTUtils.SaturnLabelDefaultProvider(){
//						public String getText(Object element) {
//						String laber = (String)element;
//						return laber;
//					}
//					}, res);
//					
//					if (dialog.open() == Window.OK) {
//						browser.setImage(Activator.getImage(Images.AMPLIFY_PLUS)); 
////						browser.setImage(Activator.getImage(IConstants.IMG_NODE33)); 
//						browser.setImage(Activator.getImage(Images.AMPLIFY_PLUS)); 
//						String ddd = (String) dialog.getFirstResult();
//						valueText.setText(ddd);
//					}
				}		
			}
		);

		final Label descritpionLabel = new Label(container, SWT.NONE);
		descritpionLabel.setText(Messages.getString("EditResourceDialog.description"));  //$NON-NLS-1$

		descriptionText = new Text(container, SWT.BORDER);
		descriptionText.setText(descritpion);
		descriptionText.addModifyListener(new ModifyListener() {
			public void modifyText(final ModifyEvent e) {
				dialogChanged();
			}
		});
		final GridData gd_descriptionText = new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1);
		descriptionText.setLayoutData(gd_descriptionText);
					
		setTitle(Messages.getString("EditResourceDialog.editonesource"));  //$NON-NLS-1$
		setMessage(Messages.getString("EditResourceDialog.writeparameter"));  //$NON-NLS-1$
		return area;
	}
	
	private List<String> formatPath(List<String> oldList) {
		List<String> newList = new ArrayList<String>();
		for (int i = 0; i < oldList.size(); i++) {
			String str = oldList.get(i);
			str = str.substring(str.indexOf(project.getName()));
			String[] aaa = str.split("\\\\");  //$NON-NLS-1$
			str = "/" + str.substring(str.indexOf(aaa[2])).replace("\\", "/"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
			newList.add(str);
		}
		return newList;
	}
	
	private String[] formatArray(String[] array, String extendName) {
		String[] newArray = new String[array.length];
		for (int i = 0; i < array.length; i++) {
			String str = array[i];
			String extend = str.substring(str.lastIndexOf(".") + 1);  //$NON-NLS-1$
			if (extendName.equals(extend)) {
				newArray[i] = str;
			}
		}
		return newArray;
	}

	/**
	 * Create contents of the button bar
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, Messages.getString("EditResourceDialog.confirm"), true);  //$NON-NLS-1$
		createButton(parent, IDialogConstants.CANCEL_ID, Messages.getString("EditResourceDialog.cancel"), false);  //$NON-NLS-1$
		dialogChanged();
	}
	
	private void dialogChanged() {
		if (SaturnValidation.isEmpty(nameText.getText())) {
//		SaturnValidation
//		if (Validation.isEmpty(nameText.getText())) {
			setErrorMessage(Messages.getString("EditResourceDialog.srcnamelimit"));  //$NON-NLS-1$
			this.getButton(IDialogConstants.OK_ID).setEnabled(false);
			return;
		}
		if (SaturnValidation.isBadWord(nameText.getText())) {
			setErrorMessage(Messages.getString("AddResourceDialog.12"));  //$NON-NLS-1$
			this.getButton(IDialogConstants.OK_ID).setEnabled(false);
			return;
		}
		if (SaturnValidation.isEmpty(valueText.getText())) {
			setErrorMessage(Messages.getString("AddResourceDialog.13"));  //$NON-NLS-1$
			this.getButton(IDialogConstants.OK_ID).setEnabled(false);
			return;
		}
		setErrorMessage(null);
		this.getButton(IDialogConstants.OK_ID).setEnabled(true);
	}

	/**
	 * Return the initial size of the dialog
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(500, 375);
	}
	
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText(Messages.getString("EditResourceDialog.editsource"));  //$NON-NLS-1$
	}
	
	protected void buttonPressed(int buttonId) {
		if (buttonId == IDialogConstants.OK_ID) {
			i = 1;
			name = nameText.getText();
			value = valueText.getText();		
			descritpion = descriptionText.getText();
		}
		super.buttonPressed(buttonId);
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getDescritpion() {
		return descritpion;
	}

	public void setDescritpion(String descritpion) {
		this.descritpion = descritpion;
	}

}
